﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class DaItemDao
    {


        public DaItemDao()
        {

            Mapper.CreateMap<Y_DaItem, DaItemModel>();
        }
        public List<Y_DaItem> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_DaItem.Count();

                return edm.Y_DaItem.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public DaItemModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_DaItem.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_DaItem, DaItemModel>(query);
            }

        }


        public int Save(DaItemModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_DaItem.FirstOrDefault(s => s.ProjectId == obj.ProjectId&&s.ItemId==obj.ItemId);

                if (query == null)
                {


                    query = new Y_DaItem();

                    query.CreateTime = obj.CreateTime;

                    edm.AddToY_DaItem(query);

                }


                query.ProjectId = obj.ProjectId;

                query.ItemId = obj.ItemId;

                query.Status = obj.Status;

                query.Remark = obj.Remark;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_DaItem.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

